﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PwdMgr.DataAccess.Exps
{
    internal class ExpBrowser
    {
        public string Browse(Exp exp)
        {
            switch(exp.Type)
            {
                case ExpType.Constants:
                    return Browse((ConstantsExp)exp);
                case ExpType.Binary:
                    return Browse((BinaryExp)exp);
                default:
                    throw new NotSupportedException(exp.Type.ToString());
            }
        }
        
        private string Browse(ConstantsExp exp)
        {
            object value = exp.Value;
            if (value == null)
                return "null";
            Type type = value.GetType();
            if (type == typeof(string))
                return "'" + value + "'";
            else
                return value.ToString();
        }

        private string Browse(BinaryExp exp)
        {
            return string.Format("({0} {1} {2})",
                Browse(exp.LeftExp),
                exp.Op,
                Browse(exp.RightExp));
        }
    }
}
